package com.bjsj.pro.tools;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.loader.ImageLoader;
import com.lzy.imagepicker.view.CropImageView;
import com.bjsj.pro.R;
import com.bjsj.pro.view.GlideRoundTransform;
import com.bjsj.pro.view.NineGridView.NineGridView;

import java.io.File;

public class ImagerPickerUtils
{

    public static void initImagePicker(int maxImgCount) {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(true);                           //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);     //是否按矩形区域保存
        imagePicker.setMultiMode(true);//多选
        imagePicker.setSelectLimit(maxImgCount);              //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }
    //单选
    public static void initImagePickerSingle(boolean isCrop) {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
        imagePicker.setCrop(isCrop);                           //允许裁剪（单选才有效）
        imagePicker.setSaveRectangle(true);
        imagePicker.setMultiMode(false);////图片选择模式
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }
    public static class GlideImageLoader implements ImageLoader
    {

        @Override
        public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {

            Glide.with(activity)                             //配置上下文
                    .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                    .error(R.drawable.ic_default_image)           //设置错误图片
                    .placeholder(R.drawable.ic_default_image)     //设置占位图片
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                    .into(imageView);
        }
        @Override
        public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {
            Glide.with(activity)                             //配置上下文
                    .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                    .into(imageView);
        }

        @Override
        public void clearMemoryCache() {
        }
    }
    /**
     * 九宫格Glide 加载
     */
    public static class GlideImageLoaders implements NineGridView.ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            Glide.with(context).load(url)//
                    /*.placeholder(R.drawable.pic_zhanwei_mid)//
                    .error(R.drawable.pic_zhanwei_mid)//*/
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//
                    .into(imageView);
        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }
    //轮播图
    public static class GlideImageLoaderss extends com.youth.banner.loader.ImageLoader {
        private  boolean yuanjia;


        public GlideImageLoaderss(boolean yuanjia) {
            this.yuanjia = yuanjia;
        }

        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //具体方法内容自己去选择，次方法是为了减少banner过多的依赖第三方包，所以将这个权限开放给使用者去选择
            if(yuanjia){

                Glide.with(context.getApplicationContext())
                        .load(path)
                        //  .placeholder(R.drawable.pic_zhanwei_mid)//
                        . transform(new CenterCrop(context.getApplicationContext()), new GlideRoundTransform(context.getApplicationContext(),5)).into(imageView);


               // Gm.loadCircleJImage((String)path,imageView);
            }else{
                Glide.with(context.getApplicationContext())
                        .load(path)
                      //  .placeholder(R.drawable.pic_zhanwei_mid)//
                      //  .error(R.drawable.pic_zhanwei_mid)//
                        .into(imageView);
            }


        }

//    @Override
//    public ImageView createImageView(Context context) {
//        //圆角
//        return new RoundAngleImageView(context);
//     }
    }
}
